package com.itheima.canal.juc;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;


public class TestInterrpt {

    public static void main(String[] args) throws InterruptedException {
        Interrpt interrpt = new Interrpt();
        Thread t0 = new Thread(interrpt, "t0");
        Thread t1 = new Thread(interrpt,"t1");
        String tName = Thread.currentThread().getName();

        System.out.println( tName+ "我启动了t0");
        t0.start();

        System.out.println(tName + "-我等个5秒，再启动t1");
        Thread.sleep(5000);
        t1.start();
        System.out.println(t1.getName() + "我被启动了");

        System.out.println(tName + "-t1获取不到锁，t0这货睡觉了，没释放，我等个5秒！");
        Thread.sleep(5000);
        System.out.println(tName + "-等了5秒了，不等了，把t1中断了！");
        t1.interrupt();
        System.out.println(t1.getName() + "哎呀！我调用了中断的方法");

        Thread.sleep(Long.MAX_VALUE);
    }
    
}

class Interrpt implements Runnable {

    @Override
    public void run() {
        String tName = Thread.currentThread().getName();
        System.out.println( tName + "-开始获取锁........." );
        try{
            synchronized (this){
                System.out.println(tName + "-获取到锁了！！！！");
                System.out.println(tName + "-睡觉了，睡个30秒！");
                Thread.sleep(30000);
                System.out.println(tName + "-睡醒了，干活！");
                for (int i = 0; i < 5; i++) {
                    System.out.println(tName + "：" + i);
                }
            }
            System.out.println(Thread.currentThread().getName() + ":release the lock.");
        }catch (Exception e){
            System.out.println(Thread.currentThread().getName() + ":异常可interrupted.");
        }

    }
}
